package entities;

import java.io.Serializable;

/**
 * Objecto que contem informacoes sobre um servidor
 * de base de dados
 * 
 * @author Leonardo Lino Nº45340
 * @author Diogo Pires Nº40351
 */
public class ServerInfo implements Serializable{
	private String address;
	private String bindName;
	private int port;
	private int id;
	private int idReplic;
	private int idCoordinator;
	private boolean available;
	private boolean inMaintenance;
	private Range main;
	private Range replic;
	private Range coordinator;
	
	public ServerInfo(String address, int port, int id){
		this.address = address;
		this.port = port;
		this.id = id;
		this.inMaintenance = false;
	}
	
	public ServerInfo(String serverData){
		String[] parts = serverData.split(";");
		String[] serverParts = parts[0].split(":");
		
		this.address = serverParts[0];
		this.bindName = serverParts[1];
		this.port = Integer.parseInt(serverParts[2]);
		this.id = Integer.parseInt(serverParts[3]);
		this.idReplic = Integer.parseInt(serverParts[4]);
		this.idCoordinator = Integer.parseInt(serverParts[5]);
		this.available = Boolean.parseBoolean(serverParts[6]);
		this.inMaintenance = Boolean.parseBoolean(serverParts[7]);
		
		if(!parts[1].equals("null"))
			this.main = new Range(parts[1]);
		
		if(!parts[2].equals("null"))
			this.replic = new Range(parts[2]);
		
		if(!parts[3].equals("null"))
			this.coordinator = new Range(parts[3]);
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public boolean isAvailable() {
		return available;
	}

	public void setAvailable(boolean available) {
		this.available = available;
	}
	
	public boolean isInMaintenance() {
		return inMaintenance;
	}

	public void setInMaintenance(boolean inMaintenance) {
		this.inMaintenance = inMaintenance;
	}

	public Range getMainRange() {
		return main;
	}

	public void setMainRange(Range main) {
		this.main = main;
	}

	public Range getReplicRange() {
		return replic;
	}

	public void setReplicRange(Range replic) {
		this.replic = replic;
	}

	public String getBindName() {
		return bindName;
	}

	public void setBindName(String bindName) {
		this.bindName = bindName;
	}

	public int getIdReplic() {
		return idReplic;
	}

	public void setIdReplic(int idReplic) {
		this.idReplic = idReplic;
	}

	public int getIdCoordinator() {
		return idCoordinator;
	}

	public void setIdCoordinator(int idCoordinator) {
		this.idCoordinator = idCoordinator;
	}

	public Range getCoordinatorRange() {
		return coordinator;
	}

	public void setCoordinatorRange(Range coordinator) {
		this.coordinator = coordinator;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		
		//Strings
		sb.append(address);
		sb.append(':');
		sb.append(bindName);
		sb.append(':');
		
		//Integers
		sb.append(port);
		sb.append(':');
		sb.append(id);
		sb.append(':');
		sb.append(idReplic);
		sb.append(':');
		sb.append(idCoordinator);
		sb.append(':');
		
		//Booleans
		sb.append(available);
		sb.append(':');
		sb.append(inMaintenance);
		
		//RANGES
		sb.append(';');
		if(main != null)
			sb.append(main.toString());
		else
			sb.append("null");
		
		sb.append(';');
		if(replic != null)
			sb.append(replic.toString());
		else
			sb.append("null");
		
		sb.append(';');
		if(coordinator != null)
			sb.append(coordinator.toString());
		else
			sb.append("null");
		
		return sb.toString();
	}
	
}
